<?php
class UserType
{
	public $Table="usertype";
	public $UserTypeID;
	public $UserTypeName;
	
	function loadByKey($id)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` where UserTypeID='".$id."'";
		$row=$ob->singleData($sql);
		$this->UserTypeID=$row['UserTypeID'];
		$this->UserTypeName=$row['UserTypeName'];		
	}
	function getTable($dk)
	{
		$ob=new ClassConnect();
		$sql="select * from `".$this->Table."` $dk";
		$data=$ob->mutiData($sql);
		return $data;
	}
	function travel($dk)
	{
		$arr=array();
		$data=$this->getTable($dk);
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['UserTypeID']);
		return $arr;
	}
	function insert($conn)
	{
		$ob=new ClassConnect();
		$field=array("UserTypeID","UserTypeName");
		$value=array($this->UserTypeID,$this->UserTypeName);
		return $ob->insertData($conn,$this->Table,$field,$value);
	}
	function update($conn)
	{
		$ob=new ClassConnect();
		$field=array("UserTypeID","UserTypeName");
		$value=array($this->UserTypeID,$this->UserTypeName);
		$where="UserTypeID = '".$this->UserTypeID."'";
		
		return $ob->updateData($conn,$this->Table,$field,$value,$where);			
	}
	
	function delete($conn)
	{
		$ob=new ClassConnect();		
		$where="UserTypeID = '".$this->UserTypeID."'";
		$this->deleteDetail($conn);
		return $ob->deleteData($conn,$this->Table,$where);
			
	}

	function getListModule()
	{
		$ob=new ClassConnect();
		$sql="select * from `usertype_module` where UserTypeID='".$this->UserTypeID."'";
		$data=$ob->mutiData($sql);
		$arr=array();
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['ModuleID']);
		return $arr;
	}
	function isPermission($moduleID)
	{
		$arrModule=$this->getListModule();
		return arrsearch($arrModule,$moduleID);
	}
	function cbUserType($cbname,$selectValue)
	{
		$data=$this->getTable(" WHERE UserTypeID <> 'supperadmin'");
		$cb="<select name='$cbname'>";
		for($i=0;$i<count($data);$i++)
		{
			
			$sel="";
			if($data[$i]['UserTypeID']==$selectValue)
				$sel="selected";
			$cb.="<option value='".$data[$i]['UserTypeID']."' $sel>".$data[$i]['UserTypeName']."</option>";
		}
		$cb.="</select>";
		return $cb;	
	}
	function insertDetail($conn,$arrModule)
	{
		$table="usertype_module";
		$ob=new ClassConnect();
		$field=array("UserTypeID","ModuleID");
		foreach($arrModule as $key => $val)
		{
			$value=array($this->UserTypeID,$arrModule[$key]);
			$ob->insertData($conn,$table,$field,$value);
		}
	}
	function deleteDetail($conn)	
	{
		$table="usertype_module";
		$ob=new ClassConnect();		
		$where=" `UserTypeID` = '".$this->UserTypeID."'";
		
		$this->clearPermission($conn);
		
		return $ob->deleteData($conn,$table,$where);
	}
	function updateDetail($conn,$arrModule)
	{
		
		$this->deleteDetail($conn);
		
		$this->insertDetail($conn,$arrModule);
	}
	function insertPermission($conn,$ModuleID,$arrPermissionID)
	{
		$table="usertype_module_permission";
		$ob=new ClassConnect();
		$field=array("UserTypeID","ModuleID","PermissionID");
		foreach($arrPermissionID as $key => $val)
		{
			$value=array($this->UserTypeID,$ModuleID,$arrPermissionID[$key]);
			$ob->insertData($conn,$table,$field,$value);
		}
	}
	function clearPermission($conn)
	{
		$table="usertype_module_permission";
		$ob=new ClassConnect();		
		$sql= "DELETE FROM `$table` WHERE `UserTypeID` = '".$this->UserTypeID."'";
		
		return $ob->query($conn,$sql);
	}
	function deletePermission($conn,$ModuleID)
	{
		$table="usertype_module_permission";
		$ob=new ClassConnect();		
		$sql= "DELETE FROM `$table` WHERE `UserTypeID` = '".$this->UserTypeID."' AND `ModuleID` = '$ModuleID'";
		
		return $ob->query($conn,$sql);
	}
	function updatePermission($conn,$ModuleID,$arrPermissionID)
	{
		$this->deletePermission($conn,$ModuleID);
		$this->insertPermission($conn,$ModuleID,$arrPermissionID);
	}
	function getListPermission($moduleID)
	{
		$ob=new ClassConnect();
		$sql="select * from `usertype_module_permission` where `UserTypeID` = '".$this->UserTypeID."' AND `ModuleID` = '$moduleID' ";
		$data=$ob->mutiData($sql);
		$arr=array();
		for($i=0;$i<count($data);$i++)
			array_push($arr,$data[$i]['PermissionID']);
		return $arr;
	}
	function isAllow($moduleID,$permissionID)
	{
		$arr=$this->getListPermission($moduleID);
		return arrsearch($arr,$permissionID);
	}
}

?>
